package com.jifeng.api.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.jifeng.api.config.SendSmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SendSmsUtils {

    @Autowired
    SendSmsConfig sendSmsConfig;

    public boolean sendSms(String mobile, int type, int hour, int min, int fee, int hours, int feePerhour) {
        String templateCode = "";
        String templateParam = "";
        boolean isPhone = RegexUtils.isPhone(mobile);

        /**
         * 判断手机号是否正确
         */
        if (!isPhone) {
            log.warn("path:{},errMsg:{}", "发送短信", "手机号格式错误");
            return false;
        }

        /**
         * 发送短信的类型（0 超时归还、1 欠费提醒、2 超时收费）
         */
        if (type == 0) {
            templateCode = "SMS_204111832";
            templateParam = "{\"hour\":" + "'" + hour + "'" + "}";
        } else if (type == 1) {
            templateCode = "SMS_204116792";
            templateParam = "{\"hour\":" + "'" + hour + "'" + ","+ "\"min\":" + "'" + min + "'" + ","+ "\"fee\":" + "'" + fee + "'" + "}";
        } else if (type == 2) {
            templateCode = "SMS_204126827";
            templateParam = "{\"hours\":" + "'" + hours + "'" + ","+"\"fee_per_hour\":" + "'" + feePerhour + "'" + "}";
        }

        // 配置属性
        DefaultProfile profile = DefaultProfile.getProfile(sendSmsConfig.getRegionId(), sendSmsConfig.getAccessKeyId(), sendSmsConfig.getAccessSecret());
        // 客户端
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建请求
        CommonRequest request = new CommonRequest();
        // 发送请求的方法
        request.setMethod(MethodType.POST);
        // 发送请求的域名
        request.setDomain(sendSmsConfig.getDomain());
        //系统规定参数
        request.setAction(sendSmsConfig.getAction());
        // 版本信息  已经固定  不能进行更改
        request.setVersion(sendSmsConfig.getVersion());
        /*
          必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,
         批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式；
         发送国际/港澳台消息时，接收号码格式为国际区号+号码，如“85200000000”
         */
        request.putQueryParameter("PhoneNumbers", mobile);
        // 签名
        request.putQueryParameter("SignName", sendSmsConfig.getSignName());
        // 阿里云控制台模板编号
        request.putQueryParameter("TemplateCode", templateCode);
        // 模板内需要填充参数信息
        request.putQueryParameter("TemplateParam", templateParam);
        // regionId 区域id
        request.putQueryParameter("RegionId", sendSmsConfig.getRegionId());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("发送短信:{}",response.getData());
//            ResponseEntity responseEntity = new ResponseEntity();
//            responseEntity.setMessage(GetValueUtils.getPhoneStr(response.getData(), "Message"));
//            responseEntity.setRequestId(GetValueUtils.getPhoneStr(response.getData(), "RequestId"));
//            responseEntity.setCode(GetValueUtils.getPhoneStr(response.getData(), "Code"));

            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
